<template>
  <div class="image-container">
    <div class="tag-list">
      <ul>
        <li v-for="(item, index) in tagList"
            :key="index"
            :style="{backgroundColor: item.backgroundColor}">
          {{ item.label }}
        </li>
      </ul>
    </div>
    <div class="image-list" ref="masonry">
      <div class="image-item">
        <img src="../assets/img/img1.png" alt="" @load="imageLoaded">
        <div class="author">
          <div class="avatar"></div>
          <span>Rella</span>
        </div>
      </div>
      <div class="image-item">
        <img src="../assets/img/img2.png" alt="" @load="imageLoaded">
        <div class="author">
          <div class="avatar"></div>
          <span>Rella</span>
        </div>
      </div>
      <div class="image-item">
        <img src="../assets/img/img3.png" alt="" @load="imageLoaded">
        <div class="author">
          <div class="avatar"></div>
          <span>Rella</span>
        </div>
      </div>
      <div class="image-item">
        <img src="../assets/img/img4.png" alt="" @load="imageLoaded">
        <div class="author">
          <div class="avatar"></div>
          <span>Rella</span>
        </div>
      </div>
      <div class="image-item">
        <img src="../assets/img/img5.png" alt="" @load="imageLoaded">
        <div class="author">
          <div class="avatar"></div>
          <span>Rella</span>
        </div>
      </div>
      <div class="image-item">
        <img src="../assets/img/img6.png" alt="" @load="imageLoaded">
        <div class="author">
          <div class="avatar"></div>
          <span>Rella</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent } from "vue";
import Masonry from "masonry-layout";

export default defineComponent({
  name: "ImageShow",
  setup() {
    let tagList = [
      {
        member: "ava",
        label: "向晚Ava",
        backgroundColor: "#9ac8e2"
      },
      {
        member: "bella",
        label: "贝拉Bella",
        backgroundColor: "#db7d74"
      },
      {
        member: "carol",
        label: "珈乐Carol",
        backgroundColor: "#b8a6d9"
      },
      {
        member: "diana",
        label: "嘉然Diana",
        backgroundColor: "#e799b0"
      },
      {
        member: "elieen",
        label: "乃琳Elieen",
        backgroundColor: "#576690"
      }
    ];

    return {
      tagList
    };
  },
  mounted() {
    this.reloadMasonry();
  },
  methods: {
    imageLoaded() {
      this.reloadMasonry()
    },
    reloadMasonry() {
      new Masonry(this.$refs.masonry as Element, {
        itemSelector: ".image-item",
        gutter: 20,
      });
    }
  }
});
</script>

<style lang="less" scoped>
.image-container {
  .tag-list {
    overflow: scroll;

    &::-webkit-scrollbar {
      width: 0;
      height: 0;
    }

    ul {
      list-style: none;
      display: flex;

      li {
        border-radius: 2px;
        color: #fff;
        margin-right: 8px;
        padding: 6px 10px;
        font-size: 8px;
        white-space: nowrap;
      }
    }
  }

  .image-list {
    margin-top: 18px;
    .image-item {
      margin-bottom: 6px;
      width: calc(50% - 10px);

      img {
        width: 100%;
        border-radius: 3.5px;
      }

      .author {
        display: flex;
        align-items: center;
        padding: 10px 0;
        font-size: 10px;

        .avatar {
          width: 22px;
          height: 22px;
          margin-right: 8px;
          border-radius: 50%;
          background: url("../assets/img/avatar.png") center/contain no-repeat;
        }
      }
    }
  }
}
</style>
